package com.ChenYaRou.www.controller.servlet;

import com.ChenYaRou.www.controller.GetTicketServletController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static com.ChenYaRou.www.constant.Constant.DISCOUNT;
import static com.ChenYaRou.www.constant.ShopConstant.*;

/**
 * @ClassName GetTicketServlet
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/1 13:55
 * @Version 1.0
 **/
public class GetTicketServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        GetTicketServletController getTicketServlet=new GetTicketServletController();
        PrintWriter out = resp.getWriter();
        JSONObject json = JSON.parseObject(req.getParameter("json"));
        String mode=json.getString("mode");
        if(HAS_TICKET.equals(mode)){
            //展示店铺的所有券
            out.write(getTicketServlet.getTicket(json.getInteger("shopId")));
        }else if(GET_TICKET.equals(mode)){
            //领券
            out.write(getTicketServlet.getTicket(
                    json.getInteger("shopId"),
                    json.getFloat(DISCOUNT), json.getInteger("id")));
        }else if(UPDATE_TICKET_STATE.equals(mode)){
            out.write(getTicketServlet.updateTicketState());
        }
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        doGet(req, resp);
    }
}
